package com.hlxj.license.server.controller;

import com.hlxj.license.server.util.FileUtil;
import com.hlxj.license.server.util.RSAUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @功能:【PlblicKeyController 】
 * @作者:代守诚
 * @日期:2019/5/21
 * @时间:15:34
 */
@RestController
@RequestMapping("/key")
public class KeyController {

    @RequestMapping(value = "/getPublicKey", method = RequestMethod.POST)
    public String getPubliceKey(HttpServletRequest request, HttpServletResponse response) {
        return FileUtil.readFileContent("publicKey");
    }

    @RequestMapping(value = "/generate/key", method = RequestMethod.POST)
    public void generateKey(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> keyMap = RSAUtil.genKeyPair();
            String publicKey = RSAUtil.getPublicKey( keyMap);
            FileUtil.createFile("publicKey", publicKey);

            String privateKey = RSAUtil.getPrivateKey( keyMap);
            FileUtil.createFile("privateKey", privateKey);
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
